package com.woniuxy.staff.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.woniuxy.commons.annotation.RequirePerms;
import com.woniuxy.commons.entity.Dept;
import com.woniuxy.commons.entity.PageBean;
import com.woniuxy.commons.entity.Role;
import com.woniuxy.commons.entity.Staff;
import com.woniuxy.commons.stafflog.LogRequirePerms;
import com.woniuxy.commons.utils.ResponseResult;
import com.woniuxy.commons.utils.ResultStatus;
import com.woniuxy.staff.service.StaffLogService;
import com.woniuxy.staff.service.StaffService;
import org.aspectj.lang.JoinPoint;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author author
 * @create 2021-11-10 11:05
 */
@RestController
@RequestMapping("/staff")
public class StaffController {

    @Resource
   private StaffService staffService;

    @Resource
    private StaffLogService staffLogService;

//添加员工的方法
    @RequirePerms("user:add")
    @LogRequirePerms(module = "员工模块",description = "添加员工信息")
    @PostMapping("/addStaff")
    @SentinelResource(value = "add",fallback = "fallbackaddStaff")
    public ResponseResult<Staff> logaddStaff(@RequestBody Staff staff){
        staffService.addStaff(staff);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setCode(200);
        responseResult.setMessage("添加成功");
        return responseResult;
    }

    //添加的降级方法
    public ResponseResult<Staff> fallbackaddStaff(){
        ResponseResult responseResult=new ResponseResult();
        responseResult.setCode(500);
        responseResult.setMessage("系统维护中");
        return responseResult;
    }



//得到所有的员工方法(分页)
@LogRequirePerms(module = "员工模块",description = "查询所有的员工信息")
    @RequirePerms("user:select")
    @GetMapping("/getallStaff")
    @SentinelResource(value = "get",fallback = "fallbackgetallStaff")
    public ResponseResult<PageBean<Staff>> loggetAllStaff(Staff staff, int pageIndex, int pageSize){

////        模拟异常是否降级
//        int n = new Random().nextInt(3);
//        if (n == 0){
//            //模拟运行时异常
//            throw new RuntimeException("运行时异常");
//        }
        System.out.println(staff);

        List<Staff> staffs = staffService.selectAllStaff(staff, pageIndex, pageSize);
        int i = staffService.selectStaffCountBylimit(staff);
        PageBean<Staff> pageBean=new PageBean<>();
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(i);
        pageBean.setBeanList(staffs);
        ResponseResult<PageBean<Staff>> responseResult=new ResponseResult();
        responseResult.setCode(200);
        responseResult.setMessage("查询成功");
        responseResult.setData(pageBean);
        return responseResult;
    }

    //查询所有的降级的方法
    public ResponseResult<Staff> fallbackgetallStaff(Staff staff,int pageIndex,int pageSize){
        ResponseResult responseResult=new ResponseResult();
        responseResult.setCode(500);
        responseResult.setMessage("系统维护中");
        return responseResult;
    }


    //删除员工的方法
    @LogRequirePerms(module = "员工模块",description = "删除员工信息")
    @RequirePerms("user:del")
    @DeleteMapping("/delStaff/{sid}")
    public ResponseResult<Staff> logdelStaff(@PathVariable("sid") int sid){
        staffService.delectStaffByid(sid);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setCode(200);
        responseResult.setMessage("删除成功");
        return responseResult;
    }


    //修改员工的方法
    @LogRequirePerms(module = "员工模块",description = "修改员工信息")
    @RequirePerms("user:update")
    @PutMapping("/updateStaff")
    public ResponseResult<Staff> logupdateStaff(@RequestBody Staff staff){
        staffService.updateStaff(staff);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setCode(200);
        responseResult.setMessage("修改成功");
        return responseResult;

    }


    @GetMapping("/getallRole")
    public List<Role> getallRole(){
       return staffService.getallRole();
    }


    @GetMapping("/getallDept")
    public List<Dept> getallDept(){
        return staffService.getallDept();
    }




    @GetMapping("/getStaffByName/{username}")
    public Staff getStaffByName(@PathVariable("username") String username){

        return staffService.getStaffByName(username);
    }


    @GetMapping("/getpermissionByName/{username}")
    public List<String> getpermissionByName(@PathVariable("username") String username){

        List<String> permissions = staffService.getPermissionByUsername(username);
        return permissions;

    }

    //    鉴权方法
    @GetMapping("/hasperms/{username}/{perms}")
    public ResponseResult hasPerms(
            @PathVariable("username") String username, @PathVariable("perms") String perms){
        //通过账号查询到所有的权限信息    5张表
        System.out.println("是否查询");
        System.out.println(username);
        System.out.println(perms);
        List<String> permsList = staffService.getPermissionByUsername(username);
        System.out.println(permsList);

//        List<String> permsList = Arrays.asList(
//                "user:all",
//                "user:find",
//                "user:update",
//                "user:del"
//        );
        boolean result = permsList.contains(perms);
        //
        ResponseResult responseResult = new ResponseResult();

        if (result){
            responseResult.setCode(200);
            responseResult.setStatus(ResultStatus.HAVE_PERMS);
        }else {
            responseResult.setCode(500);
            responseResult.setStatus(ResultStatus.NO_PERMS);
        }
        //返回鉴定结果
        return responseResult;
    }


    @PostMapping("/addLog")
    public void  addLog(JoinPoint joinPoint, String methodName, String module, String description){
        staffLogService.addStaffLog(joinPoint,methodName,module,description);
    }




}
